package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SServiceOrderFeedetailService;
import cn.huiyunche.base.service.mappers.SServiceOrderFeedetailMapper;
import cn.huiyunche.base.service.model.SServiceOrderFeedetail;
import cn.huiyunche.base.service.model.SServiceOrderFeedetailExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SServiceOrderFeedetailServiceImpl implements SServiceOrderFeedetailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SServiceOrderFeedetailServiceImpl.class);

    @Autowired
    private SServiceOrderFeedetailMapper sServiceOrderFeedetailMapper;

    @Override
    public long addFeeDetail(Long serviceOrderId, Long feeTypeId, String feeName, BigDecimal money, BigDecimal actualAmount) {
        LOGGER.info("addFeeDetail params : {}, {}, {}, {}", serviceOrderId, feeTypeId, feeName, money);

        if (null == serviceOrderId) {
            LOGGER.error("addFeeDetail serviceOrderId must not be null");
            throw new IllegalArgumentException("客户服务订单id不能为空");
        }
        if (null == feeTypeId) {
            LOGGER.error("addFeeDetail feeTypeId must not be null");
            throw new IllegalArgumentException("费用类型id不能为空");
        }
        if (money == null) {
            LOGGER.error("addFeeDetail feeTypeId must not be null");
            throw new IllegalArgumentException("费用金额不能为空");
        }

        SServiceOrderFeedetail detail = new SServiceOrderFeedetail();
        detail.setServiceOrderId(serviceOrderId);
        detail.setFeeTypeId(feeTypeId);
        detail.setFeeName(feeName);
        detail.setMoney(money);
        detail.setActualAmount(actualAmount);

        sServiceOrderFeedetailMapper.insertSelective(detail);
        return serviceOrderId;
    }

    @Override
    public List<SServiceOrderFeedetail> getList(List<Long> serviceOrderIds, List<Long> feeTypeIds) {
        LOGGER.info("getList params : {}", serviceOrderIds.size(), feeTypeIds.size());
        if (CollectionUtils.isEmpty(serviceOrderIds)) {
            LOGGER.error("getList serviceOrderIds must not empty");
            throw new IllegalArgumentException("服务订单id不能为空");
        }
//		if(CollectionUtils.isEmpty(feeTypeIds)){
//			LOGGER.error("getList feeTypeIds must not empty");
//			throw new IllegalArgumentException("费用类型id不能为空");
//		}

        SServiceOrderFeedetailExample example = new SServiceOrderFeedetailExample();
        SServiceOrderFeedetailExample.Criteria criteria = example.createCriteria().andServiceOrderIdIn(serviceOrderIds);
        if (CollectionUtils.isNotEmpty(feeTypeIds)) {
            criteria.andFeeTypeIdIn(feeTypeIds);
        }

        List<SServiceOrderFeedetail> list = sServiceOrderFeedetailMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

    @Override
    public int delete(Long serviceOrderId) {
        LOGGER.info("delete param {}", serviceOrderId);
        if (null == serviceOrderId) {
            LOGGER.error("delete param serviceOrderId must not be null");
            throw new IllegalArgumentException("服务订单id不能为空");
        }

        SServiceOrderFeedetailExample example = new SServiceOrderFeedetailExample();
        example.createCriteria().andServiceOrderIdEqualTo(serviceOrderId);
        int count = sServiceOrderFeedetailMapper.deleteByExample(example);

        return count;
    }


}
